package com.ubst.smartad.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSAAccGroupDO;
import com.ubst.smartad.dao.TSAGroupOfAccDO;
import com.ubst.smartad.dao.TSAUserAccountDO;
import com.ubst.smartad.logger.Debug;

public class TSAGroupOfAccMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;
	
	/** 
     * 생성자
     *   
    */ 
	public TSAGroupOfAccMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSAGroupOfAccMngImpl", log_level);
	}
	
	/**
	 * 계정별 그룹리스트(윈도우일경우 사용) – 하나의 계정에 여러 개의 그룹이 있을경우 등록
	 * @param userAccountDO
	 * @throws SQLException
	 */
	public void insertGroupOfAcc(TSAGroupOfAccDO groupOfAcc) throws SQLException {
		PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_GROUPOFACC " )
				.append(" ( ")
				.append("  SYSTEMID ,ACCOUNTID ,GNAME ,NSTATUS ,CRESULT ")
				.append(" ,LASTSYNC ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ")
				.append(" ) ");

			debug.debug("insertGroupOfAcc ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, groupOfAcc.systemid);
			pstmt.setString(idx++, groupOfAcc.accountid);
			pstmt.setString(idx++, groupOfAcc.gname);
			pstmt.setInt(idx++, groupOfAcc.nstatus);
			pstmt.setString(idx++, groupOfAcc.cresult);
			pstmt.setString(idx++, groupOfAcc.lastsync);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;
        } catch (SQLException e){
			throw e;
        } finally {
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }
	}
	
	/**
	 * 계정별 그룹리스트(윈도우일경우 사용) – 하나의 계정에 여러 개의 그룹이 있을경우 수정
	 * @param userAccountDO
	 * @throws SQLException
	 */
	public void updateGroupOfAcc(TSAGroupOfAccDO groupOfAcc) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_GROUPOFACC " )
				.append(" SET ")
				.append("  SYSTEMID=? ,ACCOUNTID=? ,GNAME=? ,NSTATUS=? ,CRESULT=? ")
				.append(" ,LASTSYNC=? ")
				.append(" WHERE SYSTEMID=? AND ACCOUNTID=? AND GNAME=? ");

			debug.debug("updateGroupOfAcc ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, groupOfAcc.systemid);
			pstmt.setString(idx++, groupOfAcc.accountid);
			pstmt.setString(idx++, groupOfAcc.gname);
			pstmt.setInt(idx++, groupOfAcc.nstatus);
			pstmt.setString(idx++, groupOfAcc.cresult);
			pstmt.setString(idx++, groupOfAcc.lastsync);
			pstmt.setString(idx++, groupOfAcc.systemid);
			pstmt.setString(idx++, groupOfAcc.accountid);
			pstmt.setString(idx++, groupOfAcc.gname);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
	
	/**
	 * 계정별 그룹리스트(윈도우일경우 사용) – 하나의 계정에 여러 개의 그룹이 있을경우 목록 조회
	 * @param userid
	 * @return
	 * @throws SQLException
	 */
	public TSAGroupOfAccDO selectGroupOfAcc(String systemid, String accountid, String gname) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSAGroupOfAccDO groupOfAccDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  SYSTEMID ,ACCOUNTID ,GNAME ,NSTATUS ,CRESULT ")
				.append(" ,LASTSYNC ")
				.append(" FROM TSA_GROUPOFACC ")
				.append(" WHERE SYSTEMID=? AND ACCOUNTID=? AND GNAME=? ");

			debug.debug("selectGroupOfAccList ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, systemid);
			pstmt.setString(idx++, accountid);
			pstmt.setString(idx++, gname);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				groupOfAccDO = new TSAGroupOfAccDO();
				groupOfAccDO.systemid = systemid;
				groupOfAccDO.accountid = accountid;
				groupOfAccDO.gname = gname;
				groupOfAccDO.nstatus = rs.getInt("NSTATUS");
				groupOfAccDO.cresult = rs.getString("CRESULT");
				groupOfAccDO.lastsync = rs.getString("LASTSYNC");
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return groupOfAccDO;
	}
	
	/**
	 * 계정별 그룹리스트(윈도우일경우 사용) – 하나의 계정에 여러 개의 그룹이 있을경우 삭제
	 * @param userid
	 * @throws SQLException
	 */
	public void deleteGroupOfAcc(String systemid, String accountid, String gname) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_GROUPOFACC " )
				.append(" WHERE SYSTEMID=? AND ACCOUNTID=? AND GNAME=? ");

			debug.debug("deleteGroupOfAcc ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, systemid);
			pstmt.setString(idx++, accountid);
			pstmt.setString(idx++, gname);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
}
